package com.mt.api.modules.controller;

import com.mt.common.response.WebResponse;
import com.mt.common.response.WebResponseUtils;
import com.mt.api.constants.enums.MobileLoginTypeEnum;
import com.mt.api.modules.domain.bo.AppPhoneLoginBo;
import com.mt.api.modules.domain.bo.PhoneRegisterBo;
import com.mt.api.modules.service.IAppLoginService;
import com.mt.api.util.ParamsValid;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * @Author: plover
 * @Date: 2022/01/02 7:57 下午
 * @Description:
 */
@RestController
@RequestMapping("/app/login")
@RequiredArgsConstructor
public class AppLoginController {

    private final IAppLoginService appLoginService;

    /**
     * 手机号登录
     *
     * @return
     */
    @PostMapping("/phone")
    @ApiOperation(value = "", notes = "手机号登录", httpMethod = "POST")
    public WebResponse login(@RequestBody AppPhoneLoginBo appPhoneLoginBo) {
        ParamsValid valid = new ParamsValid();
        valid.validNotNull("请输入手机号", appPhoneLoginBo.getPhone());
        if (Objects.equals(appPhoneLoginBo.getLoginType(), MobileLoginTypeEnum.CODE.getCode())) {
            valid.validNotNull("请输入验证码", appPhoneLoginBo.getCode());
        } else if (Objects.equals(appPhoneLoginBo.getLoginType(), MobileLoginTypeEnum.PASSWORD.getCode())) {
            valid.validNotNull("请输入密码", appPhoneLoginBo.getPassword());
        }
        if (!valid.isValid()) {
            return WebResponseUtils.errorParam(valid.getMsg());
        }
        return appLoginService.mobileLogin(appPhoneLoginBo);
    }

    @PostMapping("/sendCode")
    @ApiOperation(value = "", notes = "发送验证码", httpMethod = "POST")
    public WebResponse sendCode(@RequestBody AppPhoneLoginBo appPhoneLoginBo) {
        ParamsValid valid = new ParamsValid();
        valid.validNotNull("请输入手机号", appPhoneLoginBo.getPhone());
        if (!valid.isValid()) {
            return WebResponseUtils.errorParam(valid.getMsg());
        }
        return appLoginService.sendCode(appPhoneLoginBo);
    }

    @PostMapping("/verifyCode")
    @ApiOperation(value = "", notes = "验证验证码", httpMethod = "POST")
    public WebResponse verifyCode(@RequestBody AppPhoneLoginBo appPhoneLoginBo) {
        ParamsValid valid = new ParamsValid();
        valid.validNotNull("请输入手机号", appPhoneLoginBo.getPhone());
        valid.validNotNull("验证码不能为空", appPhoneLoginBo.getCode());
        if (!valid.isValid()) {
            return WebResponseUtils.errorParam(valid.getMsg());
        }
        return appLoginService.verifyCode(appPhoneLoginBo);
    }

    @PostMapping("/register")
    @ApiOperation(value = "", notes = "注册", httpMethod = "POST")
    public WebResponse register(@RequestBody PhoneRegisterBo bo) {
        ParamsValid valid = new ParamsValid();
        valid.validNotNull("请输入手机号", bo.getPhone());
        valid.validNotNull("请输入姓名", bo.getName());
        valid.validNotNull("请输入品牌名称", bo.getBrandName());
        if (!valid.isValid()) {
            return WebResponseUtils.errorParam(valid.getMsg());
        }
        return appLoginService.register(bo);
    }

    @PostMapping("/logout")
    @ApiOperation(value = "", notes = "退出登录", httpMethod = "POST")
    public WebResponse logout() {
        return appLoginService.logout();
    }
}
